<template>
  <div class="login_container">
    <div class="login_box" :style="{'backgroundColor':isDark?'#111':'#fff'}">
      <h4 v-motion-roll-bottom class="login_title">通用后台管理系统</h4>
      <el-form v-motion-fade
        ref="loginFormRef"
        :model="loginRuleForm"
        :rules="loginRules"
        class="demo-ruleForm"
        status-icon
      >
        <el-form-item prop="username">
          <el-input v-motion-roll-left
            v-model="loginRuleForm.username"
            placeholder="请输入你的用户名"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><User /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-motion-roll-left
            v-model="loginRuleForm.password"
            placeholder="请输入你的密码"
            type="password"
          >
            <template #prefix>
              <el-icon class="el-input__icon"><Lock /></el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <mi-captcha
            v-motion-roll-left
            ref="captcha"
            width="100%"
            :height="38"
            :radius="6"
            :bgColor="isDark?'#fff':'#111'"
            :textColor="isDark?'#111':'#fff'"
            border-color="#2F9688"
            :logo="logoImg"
            @success="captchaSuccess"
          />
        </el-form-item>
        <el-form-item class="login_btn" v-motion-roll-left>
          <el-button type="primary" @click="onSubmit" ref="btnLoading">登录</el-button>
          <el-button>重置</el-button>
        </el-form-item>
      </el-form>
      <!-- 主体切换 -->
      <Theme class="theme" />
    </div>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useStore } from 'vuex';
import { useRouter } from 'vue-router'
import { useMessage } from '@/hooks/useMessage';
import { useDark, useToggle } from '@vueuse/core'
import Theme from '@/layout/components/Header/components/Theme.vue';

const isDark = useDark()
console.log(isDark);

const ElMessage=useMessage();
//使用hooks拿到store
const store=useStore();
//使用hooks拿router
const router=useRouter();

import {
  login
} from '@/api/login';
//滑动验证码是否校验成功
const captchaFlag=ref(false);

const logoImg = require("@/assets/logo.png");
//表单的引用对象
const loginFormRef = ref(null);

//登录按钮的加载状态
const btnLoading=ref(false);

const loginRuleForm = reactive({
  username: "admin",
  password: "admin888",
});

//登录时的校验规则
const loginRules = reactive({
  username: [
    { required: true, message: "请输入你的用户名", trigger: "blur" },
    { min: 5, max: 11, message: "用户名的长度是5-11位", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入你的密码", trigger: "blur" },
    { min: 6, max: 12, message: "密码的长度是6-12位", trigger: "blur" },
  ],
});

//点击登录按钮
const onSubmit = async() => {
  if(captchaFlag.value){
    try {
      // let res=await login(loginRuleForm);
      // console.log('@@',res);
      if (!loginFormRef.value) return;
      await loginFormRef.value.validate(async (valid, fields) => {
        //表单校验通过
        btnLoading.value=true;
        if (valid) {
          //发送ajax向后端提交数据
          // let results=await login(loginRuleForm);
          // console.log(results);
          //向仓库派发登录的action
          let results=await store.dispatch('loginAction',loginRuleForm);
          if(results){
            router.push('/');
            ElMessage({
              showClose: true,
              message: '欢迎来到主页',
              type: 'success',
            })
          }
        } else {
          console.log('不可以提交', fields)
        }
      })
    } catch (error) {
      console.log('###','校验未通过');
    } finally{
      //关闭loading动画效果
      btnLoading.value=false;       
    }
  }else{
    ElMessage({
      showClose: true,
      message: '请点击验证码进行校验',
      type: 'error',
    })
  }
};
//滑块验证成功之后的回调
const captchaSuccess = () => {
  captchaFlag.value=true;
  // alert("滑块验证成功之后的回调");
};
</script>

<style lang="scss" scoped>
.login_container {
  width: 100%;
  height: 100%;
  background-color: #acb1bd;
  position: relative;
  .login_box {
    width: 400px;
    height: 300px;
    position: absolute;
    box-shadow: 3px 3px 10px #ccc;
    border-radius: 5px;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    .login_title {
      color: #333;
      text-align: center;
      line-height: 84px;
    }
  }
  .el-form {
    width: 90%;
    margin-left: 5%;
  }
  .login_btn {
    text-align: center;
  }
}
.theme {
  width: 38px;
  height: 40px;
  position: absolute;
  right: 10px;
  top: 0px;
}
</style>
